package com.example.restaurantclient.Fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restaurantclient.Adapter.CustomNewsAdapter;
import com.example.restaurantclient.Entities.News;
import com.example.restaurantclient.R;

import java.util.ArrayList;
import java.util.List;

//
public class NewsFragment extends Fragment {
    private View view;
    private ListView listView;
    private List<News> newItems=new ArrayList<>();
    private TextView allRead;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view==null) {
            view = inflater.inflate(R.layout.fragment_news, container, false);
            //获取控件
            listView=view.findViewById(R.id.news_list);
            allRead=view.findViewById(R.id.all_read);
            initData();
            final CustomNewsAdapter customNewsAdapter=new CustomNewsAdapter(getContext(),newItems,R.layout.news_list_item);
            listView.setAdapter(customNewsAdapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    newItems.get(position).setFlag(true);
                    customNewsAdapter.notifyDataSetChanged();
                    Toast.makeText(getContext(), "已读", Toast.LENGTH_SHORT).show();
                }
            });
            allRead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int i=0;i<newItems.size();i++){
                        newItems.get(i).setFlag(true);
                        customNewsAdapter.notifyDataSetChanged();
                    }
                    Toast.makeText(getContext(), "全部已读", Toast.LENGTH_SHORT).show();
                }
            });
        }
        return  view;
    }
    @Override
    public void onStart() {

        super.onStart();
    }

    //初始化数据
    public void initData(){
        News news1=new News("国泰宏城店","5月4日","10:12","        尊敬的顾客，您好：您于2022-05-04 10:12取消了本次排号订单，同样感谢您对我们国泰宏城店的支持。",false);
        News news2=new News("新世界店","5月2日","20:32","       您预约的新世界店2022-02-02晚市的小桌排队号49号，排号成功！请及时落座就餐，谢谢！",false);
        News news3=new News("顺康路店","4月29日","14:57","        尊敬的顾客，您好：您于2022-04-29 14:57取消了本次排号订单，同样感谢您对我们顺康路店的支持。",false);
        newItems.add(news1);
        newItems.add(news2);
        newItems.add(news3);

    }
}
